package com.example.project.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.example.project.dao.CandidateDAO;
import com.example.project.entity.Candidate;
import com.example.project.entity.CandidateForResult;
import com.example.project.entity.VoteRecord;
import com.example.project.result.Result;
import com.example.project.service.IVoteRecordService;
import com.example.project.utils.DownExcel;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;




import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class VoteRecordController {

    @Autowired
    CandidateDAO candidateDAO;

    @Autowired
    IVoteRecordService voteRecordService;


    @PostMapping(value = {"/vote/single/{captcha}/{captcha_key}","/vote/single/{captcha_key}"})
    public Result singleVote(@PathVariable(value = "captcha",required = false) String captcha,
                             @PathVariable(value = "captcha_key",required = false) String captchaKey,
                             @RequestBody VoteRecord voteRecord){
        return voteRecordService.singleVote(captcha,captchaKey,voteRecord);
    }

    @PostMapping(value = {"/vote/multiple/{captcha}/{captcha_key}","/vote/multiple/{captcha_key}"})
    public Result multipleVote(@PathVariable(value = "captcha",required = false) String captcha,
                               @PathVariable(value = "captcha_key",required = false) String captchaKey,
                               @RequestBody JSONObject voteData){
        System.out.println("voteData === > ");
        System.out.println(voteData);
        return voteRecordService.multipleVote(captcha,captchaKey,voteData);
    }

    //导出为Excel
    @RequestMapping("/download-result/{activityId}")
    public void getExcel(@PathVariable("activityId")String activityId, HttpServletResponse response) throws IllegalAccessException, IOException,
            InstantiationException {
        List<Candidate> candidates = candidateDAO.listRankCandidate(activityId);
        List<CandidateForResult> listResult = new ArrayList<>();
        int rank = 1;
        for(Candidate c : candidates){
            CandidateForResult candidateForResult = new CandidateForResult();
            candidateForResult.setRank(rank++);
            candidateForResult.setId(c.getId());
            candidateForResult.setNum(c.getNum());
            candidateForResult.setTitle(c.getTitle());
            candidateForResult.setVoteCount(c.getVoteCount());
            candidateForResult.setAid(c.getAid());
            candidateForResult.setCoverUrl(c.getCoverUrl());
            candidateForResult.setItemDesc(c.getItemDesc());
            candidateForResult.setCreateTime(c.getCreateTime());
            candidateForResult.setUpdateTime(c.getUpdateTime());
            listResult.add(candidateForResult);
        }
        DownExcel.download(response, CandidateForResult.class,listResult);
    }
}
